package com.example.demo2.service;


import com.example.demo2.dao.PersonRepository;
import com.example.demo2.entity.Person;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 */
@Service
@Transactional(rollbackOn = Exception.class)
public class PersonService {
    @Resource
    private PersonRepository personRepository;
    @Resource
    private Gson gson;
    private static final Logger logger = LoggerFactory.getLogger(PersonService.class);

    public List<Person> findAllPerson() {
        logger.info("查询所有的用户信息");
        return personRepository.findAllPerson();
    }

    public String findOne(int id) {
        logger.info("查询单个用户信息" + id);
        return gson.toJson(personRepository.findOne(id));

    }

    public String delete(int id) {
        personRepository.deleteById(id);
        logger.info("删除单个用户信息" + id);
        return gson.toJson(new Person());
    }

    public String save(Person person) {
        logger.info("保存单个用户信息" + gson.toJson(person));
        person.setDate(new Date());
        return gson.toJson(person);
    }
}
